#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;

class Solution {
public:
    int numEnclaves(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        queue<pair<int, int>> Q;
        // for(int i=0;i<m;++i)
        // {
        //     if(grid[i][0]==1) Q.emplace(i,0);
        //     if(grid[i][n-1]==1) Q.emplace(i,n-1);```
        // }
        // for(int j=1;j<n-1;++j)
        // {
        //     if(grid[0][j]) Q.emplace(0,j);
        //     if(grid[m-1][j]) Q.emplace(m-1,j);
        // }
        vector<vector<bool>> vis(m, vector<bool>(n));
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                if (i == 0 || j == 0 || i == m - 1 || j == n - 1)
                    if (grid[i][j] == 1)
                    {
                        vis[i][j] = true;
                        Q.emplace(i, j);
                    }

        int dics[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
        while (Q.size())
        {
            auto [a, b] = Q.front();
            Q.pop();
            for (auto dic : dics)
            {
                int x = a + dic[0], y = b + dic[1];
                if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 1)
                {
                    vis[x][y] = true;
                    Q.emplace(x, y);
                }
            }
        }
        int ret = 0;
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (grid[i][j] == 1 && !vis[i][j])
                    ++ret;
            }
        }
        return ret;
    }

    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        int m = isWater.size(), n = isWater[0].size();
        queue<pair<int, int>> Q;
        vector<vector<int>> ret(m, vector<int>(n, -1));

        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (isWater[i][j] == 1)
                {
                    ret[i][j] = 0;
                    Q.emplace(i, j);
                }
            }
        }
        int dics[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };
        while (!Q.empty())
        {
            auto [a, b] = Q.front();
            Q.pop();
            for (auto dic : dics)
            {
                int x = a + dic[0], y = b + dic[1];
                if (x >= 0 && x < m && y >= 0 && y < n && ret[x][y] == -1)
                {
                    ret[x][y] = ret[a][b] + 1;
                    Q.emplace(x, y);
                }
            }
        }
        return ret;
    }

    int maxDistance(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        queue<pair<int, int>> Q;
        vector<vector<int>> ret(m, vector<int>(n, -1));

        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (grid[i][j] == 1)
                {
                    ret[i][j] = 0;
                    Q.emplace(i, j);
                }
            }
        }
        int ans = -1;
        int dics[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };
        while (!Q.empty())
        {
            auto [a, b] = Q.front();
            Q.pop();
            for (auto dic : dics)
            {
                int x = a + dic[0], y = b + dic[1];
                if (x >= 0 && x < m && y >= 0 && y < n && ret[x][y] == -1)
                {
                    ret[x][y] = ret[a][b] + 1;
                    ans = max(ans, ret[x][y]);
                    Q.emplace(x, y);
                }
            }
        }
        return ans;
    }

    bool canFinish(int n, vector<vector<int>>& prerequisites) {
        unordered_map<int, vector<int>> edges;
        vector<int> in(n);
        queue<int> Q;
        for (auto& e : prerequisites)
        {
            edges[e[1]].push_back(e[0]);
            ++in[e[0]];
        }
        for (int i = 0; i < n; ++i)
        {
            if (in[i] == 0)
                Q.push(i);
        }
        while (Q.size())
        {
            int num = Q.front();
            Q.pop();
            for (auto e : edges[num])
            {
                --in[e];
                if (in[e] == 0)
                    Q.push(e);
            }
        }
        for (int i = 0; i < n; ++i)
            if (in[i])
                return false;
        return true;
    }
};